package org.charts.basic;

// *************************************************************************/
// Charada Soft
// Area de Desarrollo de Sistemas de Información
// *************************************************************************/
// Proyecto : java creator charts
// Archivo : ChartXY.java
// Descripcion : Clase ChartXY
// *************************************************************************/
// Version : 1.0.5
// Fecha de Creación: 14 de Diciembre de 2014
// Author : Daniel Andres Castillo Pedroza
// *************************************************************************/

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.util.ArrayList;
import org.charts.objects.FunctionXY;
import org.charts.objects.PointXY;
import org.charts.interfaces.IChart;
import org.charts.interfaces.IConfiguratorChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.TickUnitSource;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class ChartXY implements IChart, IConfiguratorChart {

    // Atributos de la clase ChartXY
    
    private JFreeChart grafico;
    private XYSeriesCollection dataSet;
    private String title, nameAxeX, nameAxeY;
    private PlotOrientation orientacion;
    private boolean leyenda, url;
    private boolean informacion;
    private Font fontAxes, fontData, fontTitle;
    private Color colorBack, colorChart;
    private Color colorRank, colorDomain;

    // Constructores de la clase ChartXY
    
    public ChartXY() {
        this("ChartXY");
    }

    public ChartXY(String title) {
        initAttributes(); setTitle(title);
    }

    // Metodos de la clase ChartXY
    
    /**
     * 
     */
    private void initAttributes() {
        setNameAxeX("Eje Horizontal"); setNameAxeY("Eje Vertical");
        setBackground(Color.ORANGE); setColorChart(Color.WHITE);
        setColorDomain(Color.BLACK); setColorRank(Color.BLACK);
        setFontChart("Segoe UI Light"); informacion = true; 
        url = true;  dataSet = new XYSeriesCollection();
        setOrientation(PlotOrientation.VERTICAL); setVisibleLeyend(true);
    }

    /**
     * 
     * @param colorDomain 
     */
    public void setColorDomain(Color colorDomain) {
        this.colorDomain = colorDomain;
    }

    /**
     * 
     * @param colorRank 
     */
    public void setColorRank(Color colorRank) {
        this.colorRank = colorRank;
    }

    /**
     * 
     * @param fontAxes 
     */
    public void setFontAxes(Font fontAxes) {
        this.fontAxes = fontAxes;
    }

    /**
     * 
     * @param fontData 
     */
    public void setFontData(Font fontData) {
        this.fontData = fontData;
    }

    /**
     * 
     * @param fontTitle 
     */
    public void setFontTitle(Font fontTitle) {
        this.fontTitle = fontTitle;
    }

    /**
     * 
     * @param nameAxeX 
     */
    public void setNameAxeX(String nameAxeX) {
        this.nameAxeX = nameAxeX;
    }

    /**
     * 
     * @param nameAxeY 
     */
    public void setNameAxeY(String nameAxeY) {
        this.nameAxeY = nameAxeY;
    }

    /**
     * 
     * @param orientation 
     */
    public void setOrientation(PlotOrientation orientation) {
        this.orientacion = orientation;
    }

    /**
     * 
     * @param function 
     */
    public void addFunction(FunctionXY function) {
        dataSet.addSeries(function.getFunction());
    }
    
    /**
     * 
     * @param nameFunction
     * @param points 
     */
    public void addFunction(String nameFunction, ArrayList <PointXY> points) {
        addFunction(FunctionXY.createFunction(nameFunction,points));
    }

    /**
     * 
     * @param function 
     */
    public void addFunction(XYSeries function) {
        dataSet.addSeries(function);
    }

    /**
     * 
     */
    public void discretizeRank() {
        NumberAxis rangeEjeX = (NumberAxis) grafico.getXYPlot().getRangeAxis();
        TickUnitSource unitSource = NumberAxis.createIntegerTickUnits();
        rangeEjeX.setStandardTickUnits(unitSource); // Ejecutando la Función
    }

    // Metodos sobreescritos de la clase ChartXY
    
    @Override
    public JFreeChart getChart() {
        return grafico;
    }

    @Override
    public Image getImage() {
        return grafico.getBackgroundImage();
    }

    @Override
    public final void setTitle(String title) {
        this.title = title;
    }

    @Override
    public void setBackground(Color colorBack) {
        this.colorBack = colorBack;
    }

    @Override
    public void setColorChart(Color colorChart) {
        this.colorChart = colorChart;
    }

    @Override
    public void createChart() {
        grafico = ChartFactory.createXYLineChart(null,nameAxeX,nameAxeY,dataSet,orientacion,leyenda,informacion,url);
        grafico.setBackgroundPaint(colorBack); XYPlot plot = grafico.getXYPlot();
        grafico.setTitle(new TextTitle(title,fontTitle));
        plot.setBackgroundPaint(colorChart); plot.setDomainGridlinesVisible(true);
        plot.setDomainGridlinePaint(colorDomain); plot.setRangeGridlinePaint(colorRank);
        ValueAxis labelX = plot.getDomainAxis(); labelX.setLabelFont(fontAxes);
        labelX.setTickLabelFont(fontData); ValueAxis labelY = plot.getRangeAxis();
        labelY.setLabelFont(fontAxes);  labelY.setTickLabelFont(fontData);
    }

    @Override
    public void setFontChart(String nameFont) {
        setFontAxes(new Font(nameFont,Font.PLAIN,18)); setFontData(new Font(nameFont,Font.PLAIN,18));
        setFontTitle(new Font(nameFont,Font.PLAIN,24));
    }

    @Override
    public void setVisibleLeyend(boolean leyenda) {
        this.leyenda = leyenda;
    }
}